package com.tcpengine.client;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

import com.tcpengine.net.NetService;

// TcpEngine 客户端启类
public class TcpEngineClient extends JFrame implements ActionListener {
	
	JRadioButton m_radioWs = new JRadioButton("websocket");
	JRadioButton m_radioTcp = new JRadioButton("tcp");
	ButtonGroup m_netRadioGroup = new ButtonGroup();
	NetService m_netService = null;	 // 网络服务实现
	
	String m_strWsUrl = "ws://demo.tcpengine.cn:8888";
	String m_strTcpUrl = "demo.tcpengine.cn:8889";
	JTextField m_rulTextField = new JTextField();
	
	JButton m_btnConnect = new JButton("连接");
	JButton m_btn1000 = new JButton("发送1000");
	JButton m_btn1001 = new JButton("发送1001");
	
	
	JButton jb1 = new JButton("点我自动换行");
	JButton jb2 = new JButton("点我不换行");
	private LogView m_logView = new LogView();
	private JScrollPane js = new JScrollPane(m_logView);
	
	@Override
	public void paint(Graphics g) {
		js.setBounds(5, 80, this.getWidth() - 30, this.getHeight() - 135);
		super.paint(g);
	}

	public TcpEngineClient() {
		this.setBounds(100, 100, 1024, 768);
		int btnY = 10;
		
		Font font = new Font("alias", Font.PLAIN, 15);
		JLabel netTypeLabel = new JLabel("选择网络连接类型：");
		this.setFont(font);
		netTypeLabel.setFont(font);
		netTypeLabel.setBounds(12, btnY, 140, 20);
		this.add(netTypeLabel);
		
		m_radioWs.setBounds(12+netTypeLabel.getWidth(), btnY, 100, 20);
		m_radioWs.setFont(new Font("alias", Font.PLAIN, 16));
		this.add(m_radioWs);
		m_radioTcp.setBounds(12+netTypeLabel.getWidth() + m_radioWs.getWidth() + 5, btnY, 80, 20);
		m_radioTcp.setFont(new Font("alias", Font.PLAIN, 16));
		this.add(m_radioTcp);
		m_netRadioGroup.add(m_radioWs);
		m_netRadioGroup.add(m_radioTcp);
		m_radioWs.addActionListener(this);
		m_radioTcp.addActionListener(this);
		m_radioWs.setSelected(true);
		// m_radioTcp.setSelected(true);
		m_rulTextField.setText(m_strWsUrl);
		
		// 网络地址窗口
		m_rulTextField.setBounds(12+netTypeLabel.getWidth() + m_radioWs.getWidth() + 5 + m_radioTcp.getWidth() + 5, btnY, 300, 20);
		this.add(m_rulTextField);
		
		// 连接按钮
		m_btnConnect.setBounds(12+netTypeLabel.getWidth() + m_radioWs.getWidth() + 5 + m_radioTcp.getWidth() + 5 + m_rulTextField.getWidth() + 5, btnY, 100, 20);
		m_btnConnect.setFont(font);
		this.add(m_btnConnect);
		m_btnConnect.addActionListener(this);
		
		// 1000消息接钮
		m_btn1000.setBounds(12 + netTypeLabel.getWidth() + m_radioWs.getWidth() + 5 + m_radioTcp.getWidth() + 5 + m_rulTextField.getWidth() + 5 + m_btnConnect.getWidth() + 5, btnY, 100, 20);
		m_btn1000.setFont(font);
		this.add(m_btn1000);
		m_btn1000.addActionListener(this);
		
		// 1001消息接钮
		m_btn1001.setBounds(12 + netTypeLabel.getWidth() + m_radioWs.getWidth() + 5 + m_radioTcp.getWidth() + 5 + m_rulTextField.getWidth() + 5 + m_btnConnect.getWidth() + 5 + m_btn1000.getWidth() + 5,btnY, 100, 20);
		m_btn1001.setFont(font);
		this.add(m_btn1001);
		m_btn1001.addActionListener(this);
		
		js.setBounds(5, 80, this.getWidth() - 30, this.getHeight() - 130);
		this.add(js);
		
		
		this.setVisible(true);
		this.setTitle("TcpEngine 开发测试客户端");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		m_netService = new NetService(m_logView);
		
		
		
	}

	public void actionPerformed(ActionEvent a) {
		
		if(a.getSource() == m_radioWs) {
			m_strTcpUrl = m_rulTextField.getText();
			m_rulTextField.setText(m_strWsUrl);
		}
		else if(a.getSource() == m_radioTcp) {
			m_strWsUrl = m_rulTextField.getText();
			m_rulTextField.setText(m_strTcpUrl);
		}
		else if(a.getSource() == m_btnConnect) {
			if(m_radioWs.isSelected()) {
				m_netService.connect(NetService.WS_TYPE, m_rulTextField.getText());
			}
			else {
				m_netService.connect(NetService.TCP_TYPE, m_rulTextField.getText());
			}
		}
		else if(a.getSource() == m_btn1000) {
			m_netService.sendLogin();		// 发送登陆消息
		}
		else if(a.getSource() == m_btn1001) {
			m_netService.queryUserInfo();
		}
	}
	
	
	

	public static void main(String[] args) throws Exception {

		new TcpEngineClient();

	}

}
